//
//  UIView+SRRenderPerformance.m
//  AWFoundation
//
//  Created by 索晓晓 on 2018/4/16.
//  Copyright © 2018年 SXiao.RR. All rights reserved.
//

#import "UIView+SRRenderPerformance.h"

@implementation UIView (SRRenderPerformance)


- (void)addCircleAngleFromSize:(CGSize)size cirSize:(CGSize)cirsize
{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) byRoundingCorners:UIRectCornerAllCorners cornerRadii:cirsize];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    maskLayer.frame = CGRectMake(0, 0, size.width, size.height);
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}
- (void)addCircleAngleFromSize:(CGSize)size cirSize:(CGSize)cirsize andDirection:(RenderPerformanceCirCleDirectionOptions)directions
{
    
    UIRectCorner corner = -1;
    
    if (directions & RenderPerformanceCirCleDirectionTop){
        //上面两个角
        if (corner != -1) {
            corner =  UIRectCornerTopLeft | UIRectCornerTopRight | corner;
        }else{
            corner = UIRectCornerTopLeft | UIRectCornerTopRight;
        }
    }
    if (directions & RenderPerformanceCirCleDirectionBottom){
        //下面两个角
        if (corner != -1) {
            corner = corner |  UIRectCornerBottomLeft | UIRectCornerBottomRight;
        }else{
            corner = UIRectCornerBottomLeft | UIRectCornerBottomRight;
        }
    }
    if (directions & RenderPerformanceCirCleDirectionLeft){
        //左面两个角
        if (corner != -1) {
            corner = corner | UIRectCornerBottomLeft | UIRectCornerTopLeft;
        }else{
            corner = UIRectCornerBottomLeft | UIRectCornerTopLeft;
        }
        
    }
    if (directions & RenderPerformanceCirCleDirectionRight){
        //右面两个角
        if (corner != -1) {
            corner = corner | UIRectCornerBottomRight | UIRectCornerTopRight;
        }else{
            corner = UIRectCornerBottomRight | UIRectCornerTopRight;
        }
        
    }
    
    if (directions & RenderPerformanceCirCleDirectionTopLeft){
        //上面上左
        if (corner != -1) {
            corner = corner | UIRectCornerTopLeft;
        }else{
            corner = UIRectCornerTopLeft;
        }
        
    }
    
    if (directions & RenderPerformanceCirCleDirectionTopRight){
        //左面上右
        if (corner != -1) {
            corner = corner | UIRectCornerTopRight;
        }else{
            corner = UIRectCornerTopRight;
        }
    }
    
    if (directions & RenderPerformanceCirCleDirectionBottomLeft){
        //下面下左
//        if (corner != -1) {
//            corner = corner | UIRectCornerBottomLeft;
//        }else{
//            corner = UIRectCornerBottomRight;
//        }
        corner = UIRectCornerBottomLeft;
    }
    
    if (directions & RenderPerformanceCirCleDirectionBottomRight){
        //右面下右
        corner = UIRectCornerBottomRight;
    }
    
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) byRoundingCorners:corner  cornerRadii:CGSizeMake(cirsize.width, cirsize.height)];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    maskLayer.frame = CGRectMake(0, 0, size.width, size.height);
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

- (void)addCircleAngleWithSize:(CGSize)size
{
    [self addCircleAngleFromSize:size cirSize:size];
    
}

- (void)addCircleAngleWithSize:(CGSize)size andDirection:(RenderPerformanceCirCleDirectionOptions)directions
{
    
    [self addCircleAngleFromSize:size cirSize:CGSizeMake(size.width * 0.5, size.height * 0.5) andDirection:directions];
}

- (void)addCircleAngleWithSize:(CGSize)size ShadowColor:(UIColor * _Nullable)color
{
    if (color) {//需要有阴影
        
        CAShapeLayer *shadowLayer = [[CAShapeLayer alloc]init];
        [shadowLayer setFrame:self.bounds];
        [shadowLayer setShadowColor:[color CGColor]];
        [shadowLayer setShadowOffset:CGSizeMake(0.0f,0.0f)];
        [shadowLayer setShadowOpacity:0.2f];
        [shadowLayer setFillRule:kCAFillRuleEvenOdd];
        
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathAddRect(path,NULL,CGRectInset(self.bounds, -40, -40));
        
        
        CGPathRef someInnerPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:size.width].CGPath;
        CGPathAddPath(path,NULL, someInnerPath);
        CGPathCloseSubpath(path);
        [shadowLayer setPath:path];
        CGPathRelease(path);
        
        [[self layer]addSublayer:shadowLayer];
        
        CAShapeLayer* maskLayer = [CAShapeLayer layer];
        [maskLayer setPath:someInnerPath];
        [shadowLayer setMask:maskLayer];
        
        
    }else{
        
        [self addCircleAngleWithSize:size];
        
    }
    
    
}

- (void)addEllipseMask
{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    maskLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}


@end
